package org.ran.demo.http.socket;

import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.ran.demo.http.util.StreamUtil;
/**
 * echo 服务端
 * @author caoran
 *
 */
public class TextEchoServer {
    public static void main(String[] args) throws Exception{
        try(ServerSocket serverSocket = new ServerSocket();){
            serverSocket.bind(new InetSocketAddress(8080));
            while (true) {
                Socket s = serverSocket.accept();
                new Thread(()->{
                    try {
                        Socket socket = s;
                        InputStream in = socket.getInputStream();
                        int counter=0;
                        while (counter<3) {
                            String request = StreamUtil.readLine(in);
                            counter++;
                            System.out.println("收到:"+request);
                            StreamUtil.writeLine(request, socket.getOutputStream());
                        }
                        socket.close();
                    } catch (Exception e) {
                        
                    }
                    
                }).start();
            }
        }
    }
}
